/*
Package requests
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/3/27 14:52
@File     : password_request.go
@Project  : gohub
@Desc     :
*/
package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"gohub/app/requests/validators"
)

// ResetByPhoneRequest
// @Description: 重置密码结构体
type ResetByPhoneRequest struct {
	Phone      string `json:"phone,omitempty" valid:"phone"`
	VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
	Password   string `json:"password,omitempty" valid:"password"`
}

/**
 * ResetByPhone
 * @Description: 使用手机号重置密码表单校验
 * @param data
 * @param c
 * @return map[string][]string
 */
func ResetByPhone(data any, c *gin.Context) map[string][]string {
	//验证规则
	rules := govalidator.MapData{
		"phone":       []string{"required", "digits:11"},
		"verify_code": []string{"required", "digits:6"},
		"password":    []string{"required", "min:6"},
	}
	//错误提示
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号必填，参数名：phone",
			"digits:手机号必须为11位数字",
		},
		"verify_code": []string{
			"required:验证码必填",
			"digits:验证码必须为6位数字",
		},
		"password": []string{
			"required:密码必填",
			"min:密码至少6位",
		},
	}

	//验证请求数据
	errs := validate(data, rules, messages)

	//校验验证码
	_data := data.(*ResetByPhoneRequest)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
	return errs
}

// ResetByEmailRequest
// @Description: 邮箱重置密码请求结构体
type ResetByEmailRequest struct {
	Email      string `json:"email,omitempty" valid:"email"`
	VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
	Password   string `json:"password,omitempty" valid:"password"`
}

/**
 * ResetByEmail
 * @Description: 邮箱重置密码表单校验
 * @param data
 * @param c
 * @return map[string][]string
 */
func ResetByEmail(data any, c *gin.Context) map[string][]string {
	//校验规则
	rules := govalidator.MapData{
		"email":       []string{"required", "email", "min:4", "max:30"},
		"verify_code": []string{"required", "digits:6"},
		"password":    []string{"required", "min:6"},
	}
	//错误提示
	messages := govalidator.MapData{
		"email": []string{
			"required:邮箱必填",
			"email:邮箱格式有误",
			"min:邮箱长度必须大于4位",
			"max:邮箱长度必须小于30位",
		},
		"verify_code": []string{
			"required:验证码必填",
			"digits:验证码必须为6位数字",
		},
		"password": []string{
			"required:密码必填",
			"min:密码长度不能少于6位",
		},
	}

	//校验基本规则
	errs := validate(data, rules, messages)

	//校验验证码
	_data := data.(*ResetByEmailRequest)
	errs = validators.ValidateVerifyCode(_data.Email, _data.VerifyCode, errs)
	return errs
}
